package com.example.teacher.Controller;

import cn.hutool.json.JSONUtil;
import com.example.common.domain.Result;
import com.example.common.domain.pojo.CourseOutline;
import com.example.common.domain.pojo.Section;
import com.example.common.domain.pojo.TypeHandler.JsonToObject;
import com.example.teacher.Service.CourseOutlineService;
import com.example.teacher.Service.SectionService;
import com.example.teacher.domain.dto.SectionDto;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "课程大纲管理接口")
@RestController
@RefreshScope//时时刷新配置信息
@Slf4j  //日志  log.info("登录日志测试：");
@RequestMapping("/teachers")
@RequiredArgsConstructor
public class OutlineController {

    @Autowired
    private CourseOutlineService outlineService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private JsonToObject json;


    @ApiOperation("获取课程大纲")
    @GetMapping("/generateOutline/{courseId}")
    public Result<CourseOutline> getCourseOutline(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId) {
        log.info("获取课程大纲，课程ID：{}", courseId);
        try {
            CourseOutline outline = outlineService.teacherGetChapter(courseId);
            String teachingContentJson = JSONUtil.toJsonStr(outline.getTeachingElements());
            Map<String, List<String>> outlineMapT = json.parseToMapList(teachingContentJson);
            System.out.println(outlineMapT);
            Map<String, List<String>> outlineMap = new ObjectMapper().readValue(
                    teachingContentJson,
                    new TypeReference<Map<String, List<String>>>() {
                    }
            );
            outline.setTeachingContent(outlineMap);
            System.out.println(outlineMap);
            return Result.success(outline);
        } catch (Exception e) {
            log.error("获取课程大纲失败：", e);
            return Result.error("获取课程大纲失败：" + e.getMessage());
        }
    }

    @ApiOperation("更新课程大纲")
    @PostMapping("/changeChapter")
    public Result<CourseOutline> updateCourseOutline(
            @ApiParam(value = "课程大纲信息", required = true)
            @RequestBody CourseOutline courseOutline) {
        try {
            CourseOutline updatedOutline = outlineService.teacherChangeChapter(courseOutline);
            return Result.success(updatedOutline);
        } catch (Exception e) {
            log.error("更新课程大纲失败：", e);
            return Result.error("更新课程大纲失败：" + e.getMessage());
        }
    }


    @ApiOperation("删除课程大纲")
    @DeleteMapping("/{courseId}")
    public Result<Void> deleteCourseOutline(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable("courseId") String courseId) {
        log.info("删除课程大纲，课程ID：{}", courseId);
        try {
            outlineService.deleteCourseOutline(courseId);
            return Result.success();
        } catch (Exception e) {
            log.error("删除课程大纲失败：", e);
            return Result.error("删除课程大纲失败：" + e.getMessage());
        }
    }


    @ApiOperation("根据大纲id获取小节， (小节id 为拼接字符串 课程id+第几个人章（09  11）+第几小节 （ 05  10）)")
    @GetMapping("/getSectionsBySectionId")
    public Result<Section> getSections(
            @ApiParam(value = "课程ID,也是大纲Id", required = true)
            @RequestBody SectionDto sectionDto) {
        String sectionId = sectionDto.getCourseId() + sectionDto.getChapterId() + sectionDto.getSectionId();
        return Result.success(sectionService.getSectionBySectionId(sectionId));
    }


}
